package com.heima.controller.vm_service;



import com.heima.Utlis.AliyunOSSOperator;
import com.heima.pojo.entity.Sku;
import com.heima.pojo.vo.SkuPageVo;
import com.heima.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.naming.Name;
import java.awt.*;
import java.io.File;

@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
public class SkuController {
  @Autowired
  private AliyunOSSOperator aliyunOSSOperator;
  @Autowired
  private SkuService skuService;

    /**
     * 上传文件
     */
      @PostMapping("/fileUpload")
      public String fileUpload(MultipartFile fileName) throws Exception{
          // 添加空值检查
          if (fileName == null || fileName.isEmpty()) {
              throw new IllegalArgumentException("上传的文件不能为空");
          }
          String url = aliyunOSSOperator.upload(fileName.getBytes(), fileName.getOriginalFilename());
          return url;
    }
    /**
     * 新增商品
     */
    @PostMapping
    public Boolean save(@RequestBody Sku sku){
     Boolean result =  skuService.save(sku);
     return result;
    }
    /**
     * 分页搜索商品列表
     */
    @GetMapping("/search")
    public SkuPageVo pageSku(@RequestParam(value = "pageIndex",required = false ) Integer pageIndex,
                             @RequestParam(value = "pageSize",required = false )Integer pageSize,
                             @RequestParam(value = "skuName",required = false )String skuName){
       log.info("查询商品列表参数： {} {} {}",pageIndex,pageSize,skuName);
        SkuPageVo skuPageVo= skuService.pageSku(pageIndex,pageSize,skuName);
        return skuPageVo;
    }

    /**
     * 修改商品信息
     */
    @PutMapping("/{skuId}")
    public Boolean update(@PathVariable String skuId,@RequestBody Sku sku){
        log.info("修改商品参数 {} {} ",skuId,sku);
         Boolean result = skuService.updateSku(skuId,sku);
        return result;
    }

}





















